package cn.t.repository.impl.user;

import cn.t.converter.user.UserInfoConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.UserInfoDO;
import cn.t.model.user.UserQueryDO;
import cn.t.persistence.entity.user.UserInfoPO;
import cn.t.persistence.mapper.user.UserInfoMapper;
import cn.t.repository.user.UserInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class UserInfoRepositoryImpl extends RepositoryImpl<UserInfoMapper, UserInfoPO, UserInfoConverter, UserInfoDO> implements UserInfoRepository {


    @Override
    public List<UserInfoDO> findByModel(UserInfoDO userInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<UserInfoPO> list = getWrapper(userInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<UserInfoPO> getWrapper(UserInfoDO userInfoDO) {
        LambdaQueryChainWrapper<UserInfoPO> wrapper = lambdaQuery();
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getId())) {
            wrapper.eq(UserInfoPO::getId, userInfoDO.getId());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(userInfoDO.getGmtCreatedEnd())) {
            wrapper.between(UserInfoPO::getGmtCreated, userInfoDO.getGmtCreatedStart(), userInfoDO.getGmtCreatedEnd());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(userInfoDO.getGmtModifiedEnd())) {
            wrapper.between(UserInfoPO::getGmtModified, userInfoDO.getGmtModifiedStart(), userInfoDO.getGmtModifiedEnd());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getUserName())) {
            wrapper.eq(UserInfoPO::getUserName, userInfoDO.getUserName());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getLoginPassword())) {
            wrapper.eq(UserInfoPO::getLoginPassword, userInfoDO.getLoginPassword());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getOperatePassword())) {
            wrapper.eq(UserInfoPO::getOperatePassword, userInfoDO.getOperatePassword());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getLastLoginTime())) {
            wrapper.eq(UserInfoPO::getLastLoginTime, userInfoDO.getLastLoginTime());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getLoginErrorTimes())) {
            wrapper.eq(UserInfoPO::getLoginErrorTimes, userInfoDO.getLoginErrorTimes());
        }
        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getUserStatus())) {
            wrapper.eq(UserInfoPO::getUserStatus, userInfoDO.getUserStatus());
        } else {
            wrapper.eq(UserInfoPO::getUserStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }

        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getIsDeleted())) {
            wrapper.eq(UserInfoPO::getIsDeleted, userInfoDO.getIsDeleted());
        } else {
            wrapper.eq(UserInfoPO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()));
        }

        if (userInfoDO != null && !ObjectUtils.isEmpty(userInfoDO.getRegisterIp())) {
            wrapper.eq(UserInfoPO::getRegisterIp, userInfoDO.getRegisterIp());
        }
        return wrapper;
    }

    @Override
    public long count(UserInfoDO userInfoDO) {
        return getWrapper(userInfoDO).count();
    }

    @Override
    public boolean deleteUser(Long userId) {
        return lambdaUpdate().set(UserInfoPO::getUserStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(UserInfoPO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(UserInfoPO::getGmtModified, LocalDateTime.now()).eq(UserInfoPO::getId, userId).update();
    }

    @Override
    public UserQueryDO queryUserRule(Long deviceId, String userCarNum, Long merchantId) {
        return baseMapper.queryUserRule(deviceId, userCarNum, merchantId);
    }
}
